package com.myblog.controller;

import com.myblog.pojo.*;
import com.myblog.service.*;
import com.myblog.utils.JsonData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.URLDecoder;
import java.util.*;

@Controller
public class Articlecontroller {
    @Autowired
    private Articleservice articleservice;

    @Autowired
    private Photoservice photoservice;

    @Autowired
    private Tagservice tagservice;

    @Autowired
    private Pageservice pageservice;

    @RequestMapping("/article")
    public Object article(Model model,HttpServletRequest request){
        List<Map<String,Object>> list=new LinkedList<>();
        model.addAttribute("list",list);
        List<Article> articleList=articleservice.find();
        for(Article article:articleList) {
            Map<String,Object> map=new HashMap<>();
            map.put("name", article.getName());
            map.put("describe", article.getDescribe());
            map.put("tag", article.getTag());
            Date date = new Date(article.getTime().getTime());
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            map.put("year", c.get(Calendar.YEAR));
            map.put("month", c.get(Calendar.MONTH) + 1);
            map.put("day", c.get(Calendar.DAY_OF_MONTH));
            list.add(map);
        }
        System.out.println(list);
        List<String> stringList=new ArrayList<>();
         for(Tag tag:tagservice.findtag()){
             stringList.add(tag.getTName());
         }
         model.addAttribute("tag",stringList);

         List<String> pagelist=new ArrayList<>();
         for(Article article:articleList){
             int id=(int)article.getId();
             Page page=pageservice.findpagepath(id);
             pagelist.add(page.getArticlePath());
         }
         model.addAttribute("pagelist",pagelist);
//         System.out.println(pagelist);

         List<String> photopath=new ArrayList<>();
         for(Article article:articleList){
             int id=(int)article.getId();
             Photo photo=photoservice.findpathbyid(id);
             photopath.add(photo.getPath());
         }
         model.addAttribute("photopath",photopath);
         System.out.println(photopath);

        HttpSession session=request.getSession();
        Wechat wechat=(Wechat)session.getAttribute("wechat");
        if(wechat!=null){
            System.out.println(wechat);
//            model.addAttribute("wxname",wechat.getWxname().toString());
            model.addAttribute("wximage",URLDecoder.decode(wechat.getImageurl().toString()));
            model.addAttribute("wxname",wechat.getWxname());
        }

         return "article";
    }


    @Autowired
    private Wechatservice wechatservice;

    @RequestMapping("/wximformation")
    @ResponseBody
    public Object wxphoto(){
        List<Wechat> wechatList=wechatservice.findwxphoto();
        List<String> wximformation=new ArrayList<>();
        for(Wechat wechat:wechatList){
            wximformation.add(URLDecoder.decode(wechat.getImageurl().toString()));
            wximformation.add(wechat.getWxname().toString());
        }
        return JsonData.buildSuccess(wximformation);
    }

}
